package org.dragonnova.business.web;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.dragonnova.business.model.DeviceConfig;
import org.dragonnova.business.modules.service.TagRemoteService;
import org.dragonnova.business.service.DeviceConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.base.pub.util.DateUtils;
import com.base.pub.web.BaseController;

@Controller
@RequestMapping("/tag")
public class TagConfigController extends BaseController {
    @Autowired
    private TagRemoteService tagRemoteService;
    @Autowired
    private DeviceConfigService deviceConfigService;

    /**
     * 删除
     *
     * @param session 设备编号
     * @return
     */
    @RequestMapping("/addConfig")
    @ResponseBody
    public Map<String, Object> addConfig(HttpSession session, @Validated DeviceConfig config, BindingResult result) {
        if (result.hasErrors()) {
            return bindingResult(result);
        }
        Map<String, Byte> data = new HashMap<>();
        data.put(TagRemoteService.TAG_AWAKEUP_CYCLE_CONFIG, config.getWakeup_time());
        data.put(TagRemoteService.ALARM_INTERVAL_CONFIG, config.getAlarm_time());
        data.put(TagRemoteService.LOCAL_ALARM_1, config.getAlarm_time1());
        data.put(TagRemoteService.LOCAL_ALARM_2, config.getAlarm_time2());
        data.put(TagRemoteService.DATA_REPORT_CYCLE_CONFIG, config.getDat_report_time());
        data.put(TagRemoteService.GPS_REPORT_CYCLE_CONFIG, config.getGps_report_time());
        data.put(TagRemoteService.SERVICE_STATUS_CONFIG, config.getService_status());
        config.setUpdate_time(DateUtils.getTimes(new Date(), "yyyy-MM-dd HH:mm:ss"));
        try {
            if (tagRemoteService.configureDevice(config.getBoxId(), data)) {
                return packSaveInfo(deviceConfigService.saveOrUpdateConfig(config));
            } else {
                return packSaveInfo(false);
            }
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> map = new HashMap<>();
            map.put("result", -3);
            map.put("Exception", e.getCause().getMessage());
            map.put("message", e.getCause().getMessage());
            return map;
        }
    }

    /**
     * 查询最新devicebox
     *
     * @param boxId = box_sn
     * @return
     * @throws Exception
     */
    @RequestMapping("/findTagConfig")
    @ResponseBody
    public Map<String, Object> findTagConfig(HttpSession session, @RequestParam(value = "boxId") String boxId) {
        try {
            List<DeviceConfig> list = deviceConfigService.findTagConfig(boxId);
            return packData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return warnException(e);
        }
    }

}
